package com.tj.collection.controller.chatquality.response;

import com.fqgj.common.utils.CollectionUtils;
import com.google.common.collect.Lists;
import com.kly.user.dto.UserInfoDto;
import com.kly.user.dto.chatter.Chatter;
import com.kly.user.dto.chatter.response.ChatterWorkingSetDto;
import com.kly.user.dto.report.OnlineReportDto;
import com.kly.user.dto.report.ReplyReportDto;
import com.kly.user.enums.OnlineStatus;
import com.tj.collection.db.domain.*;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Data;
import org.apache.logging.log4j.util.Strings;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.OptionalDouble;
import java.util.stream.Collectors;

@ApiModel(value = "chatterListRsp", description = "chatter列表返回参数类")
@Data
@Builder
public class ChatterListRsp implements Serializable {

    @ApiModelProperty("chatter列表")
    private List<ChatterVO> chatterList;
    @ApiModelProperty("总数")
    private Integer totalNum;

    public static ChatterListRsp toChatterListRsp(List<Chatter> chatters,
                                                  List<OnlineReportDto> onlineReportDtos,
                                                  List<ReplyReportDto> replyReportDtos,
                                                  List<UserInfoDto> userInfoDtos,
                                                  List<ChatterWorkingSetDto> chatterWorkingSets,
                                                  Integer totalNum, Map<String, RptChatterDataPreDayDto> dataReportMap) {


        final ChatterListRsp chatterListRsp = ChatterListRsp.builder().build();
        List<ChatterVO> list = Lists.newArrayList();
        for (Chatter chatter : chatters) {
            final ChatterVO chatterVO = ChatterVO.builder().workingHour(chatter.getWorkingHours()).chatterUserCode(chatter.getChatterCode()).chatterAccount(chatter.getAccount()).chatterNickName(chatter.getNickName()).jobType(chatter.getJobType()).gender(chatter.getGender()).build();
            chatterVO.setOnline(chatter.getOnlineStatus());
            if (CollectionUtils.isNotEmpty(onlineReportDtos)) {

                final double onlineAvg = onlineReportDtos.parallelStream().filter(o -> o.getChatterUserCode().equals(chatter.getChatterCode())).mapToDouble(o -> o.getWorkingTimeOnlineRage() == null ? 0d : new Double(o.getWorkingTimeOnlineRage().replace("%", ""))).average().orElse(0);
                chatterVO.setOnlineRateInWorkingHour(String.format("%.2f", onlineAvg) + "%");
            }
            if (CollectionUtils.isNotEmpty(replyReportDtos)) {
                final double replyAvg = replyReportDtos.parallelStream().filter(o -> o.getChatterUserCode().equals(chatter.getChatterCode())).mapToDouble(o -> o.getReplyOnTimeRageInWorkingTimes() == null ? 0d : new Double(o.getReplyOnTimeRageInWorkingTimes().replace("%", ""))).average().orElse(0);
                chatterVO.setReplyRateInWorkingHour(String.format("%.2f", replyAvg) + "%");
            }
            if (CollectionUtils.isNotEmpty(chatterWorkingSets)) {
                final ChatterWorkingSetDto chatterWorkingSet = chatterWorkingSets.parallelStream().filter(o -> o.getUserCode().equals(chatter.getChatterCode())).findFirst().orElse(null);
                if (chatterWorkingSet != null) {
                    chatterVO.setInWorkingHour(chatterWorkingSet.getInWorkingHour());
                }
            } else {
                chatterVO.setInWorkingHour(0);

            }
            if (CollectionUtils.isNotEmpty(dataReportMap)) {
                final RptChatterDataPreDayDto chatterDataPreDayDto = dataReportMap.get(chatter.getChatterCode());
                buildChatterVo(chatterVO, chatterDataPreDayDto);
            }
            list.add(chatterVO);
        }
        chatterListRsp.setChatterList(list);
        chatterListRsp.setTotalNum(totalNum);


        return chatterListRsp;
    }

    private static void buildChatterVo(ChatterVO chatterVO, RptChatterDataPreDayDto dto) {
        if (dto == null) {
            return;
        }
        final OnlineUser onlineUser = dto.getOnlineUser();
        if (onlineUser != null) {
            chatterVO.setOnlineUser(chatterVO.getOnlineUser(onlineUser.getOnlineUserTotal(), onlineUser.getOnlineUserCnt(), onlineUser.getShowWarningFlag()));
        }
        chatterVO.setActiveChasCnt(dto.getActiveChatsCnt());
        final NewRelationCnt newRelationCnt = dto.getNewRelationCnt();
        if (newRelationCnt != null) {
            chatterVO.setNewRelation(chatterVO.getNewRelationCnt(newRelationCnt.getNewRelationCnt(), newRelationCnt.getShowWarningFlag()));
        }
        chatterVO.setUserSendMsgCnt(dto.getUserSendMsgCnt());
        chatterVO.setReplayCnt(dto.getReplyCnt());
        final BreakIceChat breakIceChat = dto.getBreakIceChatCnt();
        if (breakIceChat != null) {
            chatterVO.setBreakIceChatCnt(chatterVO.getBreakIceChat(breakIceChat.getBreakIceChatCnt(), newRelationCnt.getShowWarningFlag()));
        }
        chatterVO.setValidChatCnt(dto.getValidChatsCnt());
        chatterVO.setDeepChatCnt(dto.getDeepChatsCnt());
        chatterVO.setTotalPpCnt(dto.getTotalPpCnt());
        chatterVO.setTotalPvCnt(dto.getTotalPvCnt());
        final TotalPPv totalPPv = dto.getTotalPpvCnt();
        if (totalPPv != null) {
            chatterVO.setTotalPPvCnt(chatterVO.getTotalPPv(totalPPv.getTotalPPvCnt(), totalPPv.getShowWarningFlag()));
        }
    }

}
